package com.qttaudio.example.dialog;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.qttaudio.example.RoomActivity;
import com.qttaudio.example.data.MicSeatBean;
import com.qttaudio.ios.demo.R;


public class MicNetworkPopupWindow extends PopupWindow {
    private MicSeatBean micSeatBean;
    private int position;
    private View contentView;
    private TextView tv_mic_pop_one;


    public MicNetworkPopupWindow(Context context) {
        this(context, 0, null);
    }

    public MicNetworkPopupWindow(Context context, int position, MicSeatBean micSeatBean) {
        super(context);
        this.position = position;
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        setOutsideTouchable(true);
        setFocusable(true);
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        contentView = LayoutInflater.from(context).inflate(R.layout.layout_room_mic_item_network_pop,
                null, false);
        this.micSeatBean = micSeatBean;
        initView(contentView, position, micSeatBean);
        setContentView(contentView);
    }

    private void initView(View contentView, int position, MicSeatBean micSeatBean) {
        tv_mic_pop_one = contentView.findViewById(R.id.tv_mic_pop_one);
        if (micSeatBean != null) {
            contentView.setVisibility(View.VISIBLE);
            if (micSeatBean.getUid() == RoomActivity.mUid) {
                tv_mic_pop_one.setText("RTT:" + RoomActivity.myRtt);
            } else if (micSeatBean.getChannelUser() != null) {
                tv_mic_pop_one.setText("抖动:" + micSeatBean.getChannelUser().getJitter() + ",丢包:" + micSeatBean.getChannelUser().getLossRate() + "%\n" +
                        "时延:" + micSeatBean.getChannelUser().getDistance() + ",网速:" + (micSeatBean.getChannelUser().getCurbps() / 1024) + "KB/S");
            }
        } else {
            contentView.setVisibility(View.GONE);
        }
    }

    public void updateView(int position, final MicSeatBean micSeatBean) {
        if (position < 0 || position != this.position) {
            return;
        }
        Log.d("MicNetworkPopupWindow", "updateMyRttView position:" +position+"  this.position:"+micSeatBean);
        this.micSeatBean = micSeatBean;
        contentView.post(new Runnable() {
            @Override
            public void run() {
                if (micSeatBean != null) {
                    contentView.setVisibility(View.VISIBLE);
                    if (micSeatBean.getUid() == RoomActivity.mUid) {
                        tv_mic_pop_one.setText("RTT:" + RoomActivity.myRtt);
                    } else if (micSeatBean.getChannelUser() != null) {
                        tv_mic_pop_one.setText("抖动:" + micSeatBean.getChannelUser().getJitter() + ",丢包:" + micSeatBean.getChannelUser().getLossRate() + "%\n" +
                                "时延:" + micSeatBean.getChannelUser().getDistance() + ",网速:" + (micSeatBean.getChannelUser().getCurbps() / 1024) + "KB/S");
                    }
                } else {
                    MicNetworkPopupWindow.this.dismiss();
                }
            }
        });

    }
    public void updateMyRttView(int position) {
        Log.d("MicNetworkPopupWindow", "updateMyRttView position:" +position+"  this.position:"+this.position);
        updateView(position,micSeatBean);
    }
}